package org.balthie.demo.jdk.util.map;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
import org.springframework.util.StopWatch;

public class HashMapDemo
{
    public static void main(String[] args)
    {
        HashMapDemo demo = new HashMapDemo();
        try
        {
            demo.resize();
        }
        catch (InterruptedException e)
        {
        }
    }
    
    public void resize() throws InterruptedException
    {
        HashMap<Integer, String> m = new HashMap<Integer, String>();
        m.put(0, "value"+0);
        IntStream.range(0,12).forEach(i -> m.put(i, "value"+i));
        
        System.out.println("resize 前");
        for(Map.Entry<Integer, String> entry : m.entrySet())
        {
            System.out.println(entry.getKey() +" hashcode "+ entry.hashCode() + " 扩容前下标 =" + (entry.hashCode() & 15 ) + " hash&olcCap =" + (entry.hashCode() & 16 ) );
        }
        
//        第13个entry触发扩容
        m.put(12, "resize");
        System.out.println("resize 后");
        for(Map.Entry<Integer, String> entry : m.entrySet())
        {
            System.out.println(entry.getKey() +" hashcode "+ entry.hashCode() + " 扩容后下标 =" + (entry.hashCode() & 31 ));
        }
        
        int x =0;
        StopWatch sw = new StopWatch();
        
        sw.start("取模100w次");
        for(int j =1_000_000_000;j<2_000_000_000;j++)
        {
            x = j % 32;
        }
        sw.stop();
        
        Thread.sleep(2000L);

        sw.start("按位与100w次");
        for(int j =1_000_000_000;j<2_000_000_000;j++)
        {
            x = j &31;
        }
        sw.stop();

        System.out.println(sw.prettyPrint());
    }
}
